﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace CSI
{
	/// <summary>
	/// This class provides a custom web part editor to be used in coordination with the
	/// MonthFilterWebPart. The custom editor provides a more customized experience and
	/// more robust control set when editing the properties associated with this web part.
	/// </summary>
	/// <author>Seth Hildebrand</author>
	class MonthFilterEditor : EditorPart
	{
		#region ----- DECLARATIONS ------

		/// <summary>
		/// Reference to a text box control that is added to the web part editor to allow
		/// for modifying the label text field of the web part.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		private TextBox _labelTextBox;

		/// <summary>
		/// Reference to a text box control that is added to the web part editor to allow
		/// for modifying the date format for the date displayed in the dropdown list box.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		private TextBox _displayDateFormatTextBox;

		/// <summary>
		/// Reference to a text box control that is added to the web part editor to allow
		/// for modifying the date format that is used when the web part is connected as
		/// a connection provider.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		private TextBox _connectionProviderDateFormatTextBox;

		/// <summary>
		/// Reference to a text box control that is added to the web part editor to allow
		/// for modifying the default offset value of the web part.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		private TextBox _defaultOffsetTextBox;

		/// <summary>
		/// Reference to a text box control that is added to the web part editor to allow
		/// for modifying the start offset value of the web part.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		private TextBox _startOffsetTextBox;

		/// <summary>
		/// Reference to a text box control that is added to the web part editor to allow
		/// for modifying the number of months displayed in the dropdown list on the web part.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		private TextBox _monthCountTextBox;

		#endregion

		#region ----- METHODS -----

		/// <summary>
		/// Child controls are created dynamically and added to the web part editor so that
		/// custom controls may be added for editing.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		protected override void CreateChildControls()
		{
			//Set the title to the custom properties section.
			this.Title = "Filter Settings";

			//Create Label Text text box.
			this.Controls.Add(new LiteralControl("Label Text:<br />"));
			_labelTextBox = new TextBox();
			this.Controls.Add(_labelTextBox);
			_labelTextBox.ID = "ctlLabelText";
			_labelTextBox.Width = Unit.Percentage(90);
			this.Controls.Add(new LiteralControl("<br/><br/>"));

			//Create Display Date Format text box.
			this.Controls.Add(new LiteralControl("Display Date Format:<br />"));
			_displayDateFormatTextBox = new TextBox();
			this.Controls.Add(_displayDateFormatTextBox);
			_displayDateFormatTextBox.ID = "ctlDisplayDateFormatTextBox";
			_displayDateFormatTextBox.Width = Unit.Percentage(90);
			this.Controls.Add(new LiteralControl("<br/><br/>"));

			//Create Connection Provider Date Format text box.
			this.Controls.Add(new LiteralControl("Connection Provider Date Format:<br />"));
			_connectionProviderDateFormatTextBox = new TextBox();
			this.Controls.Add(_connectionProviderDateFormatTextBox);
			_connectionProviderDateFormatTextBox.ID = "ctlConnectionProviderDateFormatTextBox";
			_connectionProviderDateFormatTextBox.Width = Unit.Percentage(90);
			this.Controls.Add(new LiteralControl("<br/><br/>"));

			//Create Start Offset text box.
			this.Controls.Add(new LiteralControl("<span title='Establish the first month that appears in the dropdown selection by specifying the offset from the current month. A value of zero results in the current month, while a positive value would increment the start month by that many months. A negative value may also be used to show a start month that is less than the current month.'>Start Offset:</span><br />"));
			_startOffsetTextBox = new TextBox();
			this.Controls.Add(_startOffsetTextBox);
			_startOffsetTextBox.ID = "ctlStartOffset";
			_startOffsetTextBox.Width = Unit.Percentage(90);
			this.Controls.Add(new LiteralControl("<br/><br/>"));

			//Create Default Offset text box.
			this.Controls.Add(new LiteralControl("<span title='Establish the default month that appears in the dropdown selection by specifying the offset from the current month. A value of zero results in the current month, while a positive value would increment the default month to the future from today's month. A negative value may also be used to show a default month that is less than the current month.'>Default Offset:</span><br />"));
			_defaultOffsetTextBox = new TextBox();
			this.Controls.Add(_defaultOffsetTextBox);
			_defaultOffsetTextBox.ID = "ctlDefaultOffset";
			_defaultOffsetTextBox.Width = Unit.Percentage(90);
			this.Controls.Add(new LiteralControl("<br/><br/>"));

			//Create Month Count text box.
			this.Controls.Add(new LiteralControl("<span title='Indicate the total number of months that should appear in the dropdown selection. The first month (topmost) in the list is determined by the Start Offset value.'>Number Of Months Shown:</span><br />"));
			_monthCountTextBox = new TextBox();
			this.Controls.Add(_monthCountTextBox);
			_monthCountTextBox.ID = "ctlMonthCount";
			_monthCountTextBox.Width = Unit.Percentage(90);
			this.Controls.Add(new LiteralControl("<br/><br/>"));
		}

		/// <summary>
		/// This method pulls the values stored behind the public properties of the web part
		/// into this web part editor. It populates the visual controls on this web part
		/// editor with those values.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		public override void SyncChanges()
		{
			//Declarations

			//Ensure child controls have loaded prior to loading their values.
			EnsureChildControls();

			//Pull values from web part and assign them to the hidden state.
			MonthFilter webPart = (MonthFilter)this.WebPartToEdit;
			_labelTextBox.Text = webPart.LabelText;
			_displayDateFormatTextBox.Text = webPart.DisplayDateFormat;
			_connectionProviderDateFormatTextBox.Text = webPart.ConnectionProviderDateFormat;
			_defaultOffsetTextBox.Text = webPart.DefaultOffset.ToString();
			_startOffsetTextBox.Text = webPart.StartOffset.ToString();
			_monthCountTextBox.Text = webPart.NumberOfMonthsToDisplay.ToString();
		}

		/// <summary>
		/// This method is called when the user applies changes or save the changes made
		/// through the editor. The values a applied to the web part.
		/// </summary>
		/// <author>Seth Hildebrand</author>
		public override bool ApplyChanges()
		{
			//Declaratons
			MonthFilter webPart;

			//Apply changes to web part.
			webPart = (MonthFilter)this.WebPartToEdit;
			webPart.LabelText = _labelTextBox.Text;
			if (webPart.DisplayDateFormat != _displayDateFormatTextBox.Text)
			{
				webPart.DisplayDateFormat = _displayDateFormatTextBox.Text;
				webPart.LoadListBox();
			}
			webPart.ConnectionProviderDateFormat = _connectionProviderDateFormatTextBox.Text;
			webPart.NumberOfMonthsToDisplay = Convert.ToInt32(_monthCountTextBox.Text);
			webPart.DefaultOffset = Convert.ToInt32(_defaultOffsetTextBox.Text);
			webPart.StartOffset = Convert.ToInt32(_startOffsetTextBox.Text);

			//Indicate success.
			return true;
		}

		#endregion

	}//End Class
}//End Namespace
